package com.example.hzq.zqb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.bean.CampusShopBean;
import com.example.hzq.zqb.bean.OrderBean;
import com.example.hzq.zqb.model.Model;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.activity.LoginActivity.u_id;
import static com.example.hzq.zqb.adapter.CampusshopAdapter.goodid;
import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/10.
 */

public class OrderActivity extends AppCompatActivity implements View.OnClickListener{
    private int typeid;
    private CampusShopBean campusShopBean;
    private ImageButton sub,add,back;
    private EditText editText;
    private TextView order,tel,address,goodcontent,type,price,num,sum,sumprice,top;
    private Button enter;
    private ImageView goodpic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        init();
    }
    private void init(){
        enter=(Button)findViewById(R.id.orderbuy);
        back=(ImageButton)findViewById(R.id.back1) ;
        sub=(ImageButton)findViewById(R.id.O_sub);
        add=(ImageButton)findViewById(R.id.O_add);
        editText=(EditText)findViewById(R.id.O_e_num);
        order=(TextView)findViewById(R.id.order);
        tel=(TextView)findViewById(R.id.ordertel);
        address=(TextView)findViewById(R.id.orderaddress);
        goodcontent=(TextView)findViewById(R.id.O_goodsdetail);
        type=(TextView)findViewById(R.id.O_type);
        price=(TextView)findViewById(R.id.O_price);
        num=(TextView)findViewById(R.id.O_num);
        sum=(TextView)findViewById(R.id.O__sum);
        sumprice=(TextView)findViewById(R.id.O_sumprice);
        top=(TextView)findViewById(R.id.toptext);
        goodpic=(ImageView)findViewById(R.id.orderpic);
        top.setText("确认订单");
        Intent intent=getIntent();
        typeid=intent.getIntExtra("typeid",-1);
        campusShopBean=(CampusShopBean)getIntent().getSerializableExtra("shopbean");
        editText.setText(intent.getStringExtra("num"));
        type.setText(intent.getStringExtra("type"));
        num.setText(editText.getText().toString());
        price.setText(campusShopBean.getPrice());
        Picasso.with(OrderActivity.this).load(imageurl+campusShopBean.getHead_img()).placeholder(R.drawable.ic_default_adimage).into(goodpic);
        goodcontent.setText(campusShopBean.getDetail());
        getdata();
        int  number=Integer.parseInt(num.getText().toString());
         float oneprice=Float.parseFloat(campusShopBean.getPrice());
        sum.setText("共"+number+"件商品，小计:"+oneprice*number+"元");
        sumprice.setText("合计"+oneprice*number+"元");
        back.setOnClickListener(this);
        sub.setOnClickListener(this);
        add.setOnClickListener(this);
        enter.setOnClickListener(this);
        if (number>1){
            sub.setImageResource(R.mipmap.minus);
            sub.setClickable(true);
        }
        else {
            sub.setImageResource(R.mipmap.minus_dis);
            sub.setClickable(false);
        }
        if (number>=Integer.parseInt(campusShopBean.getCount())){
            add.setImageResource(R.mipmap.plus_dis);
            add.setClickable(false);
        }
        else {
            add.setImageResource(R.mipmap.plus);
            add.setClickable(true);
        }
    }
    private void getdata(){
        Model model=new Model();
        Call<OrderBean> call=model.subGoodsOrder(goodid,u_id,typeid,-1);
        call.enqueue(new Callback<OrderBean>() {
            @Override
            public void onResponse(Call<OrderBean> call, Response<OrderBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    address.setText("收货地址:"+response.body().getAddress().getAddress());
                    order.setText("收货地址:"+response.body().getAddress().getName());
                    tel.setText("收货地址:"+response.body().getAddress().getPhone());
                }
            }

            @Override
            public void onFailure(Call<OrderBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int  number=Integer.parseInt(num.getText().toString());
        float oneprice=Float.parseFloat(campusShopBean.getPrice());
        switch (v.getId()){
            case R.id.back1:
                finish();
                break;
            case R.id.O_add:
                number++;
                editText.setText(number+"");
                num.setText(number+"");
                sum.setText("共"+number+"件商品，小计:"+oneprice*number+"元");
                sumprice.setText("合计"+oneprice*number+"元");
                if (number>=Integer.parseInt(campusShopBean.getCount())){
                    add.setImageResource(R.mipmap.plus_dis);
                    add.setClickable(false);
                }
                else {
                    add.setImageResource(R.mipmap.plus);
                    add.setClickable(true);
                    sub.setImageResource(R.mipmap.minus);
                    sub.setClickable(true);
                }
                break;
            case R.id.O_sub:
                number--;
                editText.setText(number+"");
                num.setText(number+"");
                sum.setText("共"+number+"件商品，小计:"+oneprice*number+"元");
                sumprice.setText("合计"+oneprice*number+"元");
                if (number>1){
                    sub.setImageResource(R.mipmap.minus);
                    sub.setClickable(true);
                    add.setImageResource(R.mipmap.plus);
                    add.setClickable(true);
                }
                else {
                    sub.setImageResource(R.mipmap.minus_dis);
                    sub.setClickable(false);
                }
                break;
            case R.id.orderbuy:
                Toast.makeText(OrderActivity.this,"不会支付,请等待完善",Toast.LENGTH_SHORT).show();
                break;

        }
    }
}
